package org.jslaughter.component.pickup;

import org.jslaughter.event.eventtype.StatsEvent;
import org.nvframe.component.property.PropertyManager;
import org.nvframe.entity.Entity;
import org.nvframe.event.EventService;
import org.nvframe.exception.NVFrameException;
import org.nvframe.exception.NVFrameRuntimeException;
import org.nvframe.manager.ConfigManager;
import org.nvframe.util.settings.SettingsObj;

public class StatsPickup extends AbstractPickup {
	
	private String affectsProperty;
	private String affectOperation;
	private int affectsValue;
	
	public StatsPickup(String id, Entity owner, SettingsObj settings) throws NVFrameException {
		super(id, owner, settings);		
		
		affectsProperty = settings.getString("affectsProperty", null);
		affectOperation = settings.getString("affectOperation", null);
		affectsValue = settings.getInt("affectsValue", 0);
		
		if(affectsProperty == null || affectOperation == null)
			throw new NVFrameException("cannot instantiate PickupableImpl component without " +
					"a affectsProperty or affectOperation in settings [entityid: " + owner.getId() + "]");
	}

	/**
	 * Set the transient property of a given Entity
	 * 
	 * @param entity The Entity who receives the pickup
	 */
	public boolean pickupAction(Entity receiver) {
		PropertyManager propManagerComp = (PropertyManager) receiver.getComponent(PropertyManager.class);
		
		if(propManagerComp == null)
			throw new NVFrameRuntimeException("pick-up: PropertyManager not found for receiver: " + receiver.getId());
		
		if(affectOperation.equals("add"))
			propManagerComp.getPropertyInt(affectsProperty).add(affectsValue);
		else if(affectOperation.equals("substract"))
			propManagerComp.getPropertyInt(affectsProperty).substract(affectsValue);
		else if(affectOperation.equals("multiply"))
			propManagerComp.getPropertyInt(affectsProperty).multiply(affectsValue);
		else if(affectOperation.equals("percentage"))
			propManagerComp.getPropertyInt(affectsProperty).percentage(affectsValue);

		EventService.getInstance().fireEvent(new StatsEvent(affectsProperty), receiver);
		
		if(ConfigManager.getInstance().getActive("debugMode"))
			System.out.println("pick-up - type: stats | receiver: "+ receiver.getId() + " | affectedProperty: " + 
					affectsProperty + " | operation: " + affectOperation + " | quantity: " + affectsValue);
		
		return true;
	}


}
